/*     */ package com.zimbra.qa.unittest.prov.ldap;
/*     */ 
/*     */ import com.google.common.collect.Maps;
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.account.Key.CacheEntryBy;
/*     */ import com.zimbra.common.account.Key.CosBy;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AccountServiceException;
/*     */ import com.zimbra.cs.account.Cos;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Entry;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Provisioning.CacheEntry;
/*     */ import com.zimbra.qa.QA.Bug;
/*     */ import com.zimbra.qa.unittest.prov.Names;
/*     */ import com.zimbra.soap.admin.type.CacheEntryType;
/*     */ import java.io.PrintStream;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import org.junit.AfterClass;
/*     */ import org.junit.Assert;
/*     */ import org.junit.BeforeClass;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestLdapProvCos
/*     */   extends LdapTest
/*     */ {
/*     */   private static LdapProvTestUtil provUtil;
/*     */   private static Provisioning prov;
/*     */   private static Domain domain;
/*     */   
/*     */   @BeforeClass
/*     */   public static void init()
/*     */     throws Exception
/*     */   {
/*  50 */     provUtil = new LdapProvTestUtil();
/*  51 */     prov = provUtil.getProv();
/*  52 */     domain = provUtil.createDomain(baseDomainName());
/*     */   }
/*     */   
/*     */   @AfterClass
/*     */   public static void cleanup() throws Exception {
/*  57 */     Cleanup.deleteAll(new String[] { baseDomainName() });
/*     */   }
/*     */   
/*     */   private Cos createCos(String cosName) throws Exception {
/*  61 */     return createCos(cosName, null);
/*     */   }
/*     */   
/*     */   private Cos createCos(String cosName, Map<String, Object> attrs) throws Exception {
/*  65 */     return provUtil.createCos(cosName, attrs);
/*     */   }
/*     */   
/*     */   private void deleteCos(Cos cos) throws Exception {
/*  69 */     provUtil.deleteCos(cos);
/*     */   }
/*     */   
/*     */   private void modifyAttr(Entry entry, String attr, String value) throws Exception {
/*  73 */     Map<String, Object> attrs = Maps.newHashMap();
/*  74 */     attrs.put(attr, value);
/*  75 */     prov.modifyAttrs(entry, attrs);
/*  76 */     Assert.assertEquals(value, entry.getAttr(attr, false));
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void createCos() throws Exception {
/*  81 */     String COS_NAME = Names.makeCosName(genCosName());
/*  82 */     Cos cos = createCos(COS_NAME);
/*  83 */     deleteCos(cos);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void createCosAlreadyExists() throws Exception {
/*  88 */     String COS_NAME = Names.makeCosName(genCosName());
/*  89 */     Cos cos = createCos(COS_NAME);
/*     */     
/*  91 */     boolean caughtException = false;
/*     */     try {
/*  93 */       prov.createCos(COS_NAME, new HashMap());
/*     */     } catch (AccountServiceException e) {
/*  95 */       if ("account.COS_EXISTS".equals(e.getCode())) {
/*  96 */         caughtException = true;
/*     */       }
/*     */     }
/*  99 */     Assert.assertTrue(caughtException);
/*     */     
/* 101 */     deleteCos(cos);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void defaultCos() throws Exception {
/* 106 */     Cos cos = prov.get(Key.CosBy.name, "default");
/* 107 */     Assert.assertNotNull(cos);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void getCos() throws Exception {
/* 112 */     String COS_NAME = Names.makeCosName(genCosName());
/* 113 */     Cos cos = createCos(COS_NAME);
/* 114 */     String cosId = cos.getId();
/*     */     
/* 116 */     prov.flushCache(CacheEntryType.cos, null);
/* 117 */     cos = prov.get(Key.CosBy.id, cosId);
/* 118 */     Assert.assertEquals(cosId, cos.getId());
/*     */     
/* 120 */     prov.flushCache(CacheEntryType.cos, null);
/* 121 */     cos = prov.get(Key.CosBy.name, COS_NAME);
/* 122 */     Assert.assertEquals(cosId, cos.getId());
/*     */     
/* 124 */     deleteCos(cos);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void getCosNotExist() throws Exception {
/* 129 */     String COS_NAME = Names.makeCosName(genCosName());
/* 130 */     prov.flushCache(CacheEntryType.cos, null);
/* 131 */     Cos cos = prov.get(Key.CosBy.name, COS_NAME);
/* 132 */     Assert.assertNull(cos);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void copyCos() throws Exception {
/* 137 */     String COS_NAME = Names.makeCosName(genCosName());
/* 138 */     Cos defaultCos = prov.get(Key.CosBy.name, "default");
/* 139 */     Cos copiedCos = prov.copyCos(defaultCos.getId(), COS_NAME);
/*     */     
/* 141 */     Map<String, Object> defaultCosAttrs = defaultCos.getAttrs();
/* 142 */     Map<String, Object> copiedCosAttrs = copiedCos.getAttrs();
/*     */     
/* 144 */     for (Map.Entry<String, Object> attr : defaultCosAttrs.entrySet()) {
/* 145 */       String attrName = (String)attr.getKey();
/* 146 */       Object valueInDefaultCos = attr.getValue();
/*     */       
/* 148 */       Object valueInCopiedCos = copiedCosAttrs.get(attrName);
/*     */       
/*     */ 
/* 151 */       if (!"description".equals(attrName)) {
/* 152 */         Assert.assertNotNull(valueInCopiedCos);
/*     */         
/*     */ 
/*     */ 
/*     */ 
/* 157 */         if ((valueInDefaultCos instanceof String)) {
/* 158 */           Assert.assertTrue(valueInCopiedCos instanceof String);
/* 159 */           if ((!"zimbraId".equals(attrName)) && (!"zimbraCreateTimestamp".equals(attrName)) && (!"zimbraACE".equals(attrName)) && (!"cn".equals(attrName)) && (!"description".equals(attrName)))
/*     */           {
/*     */ 
/*     */ 
/*     */ 
/* 164 */             Assert.assertEquals((String)valueInDefaultCos, (String)valueInCopiedCos);
/*     */           }
/* 166 */           else if (((String)valueInDefaultCos).equals((String)valueInCopiedCos)) {
/* 167 */             System.out.println("attr: " + attrName);
/* 168 */             System.out.println("valueInDefaultCos: " + (String)valueInDefaultCos);
/* 169 */             System.out.println("valueInCopiedCos: " + (String)valueInCopiedCos);
/* 170 */             Assert.fail();
/*     */           }
/*     */         }
/* 173 */         else if ((valueInDefaultCos instanceof String[])) {
/* 174 */           Assert.assertTrue(valueInCopiedCos instanceof String[]);
/* 175 */           Assert.assertEquals(((String[])valueInDefaultCos).length, ((String[])valueInCopiedCos).length);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 187 */     for (String attr : defaultCosAttrs.keySet()) {
/* 188 */       if (!copiedCosAttrs.containsKey(attr)) {
/* 189 */         System.out.println("Attr in defaultCosAttrs but not in copiedCosAttrs: " + attr);
/*     */       }
/*     */     }
/* 192 */     for (String attr : copiedCosAttrs.keySet()) {
/* 193 */       if (!defaultCosAttrs.containsKey(attr)) {
/* 194 */         System.out.println("Attr in copiedCosAttrs but not in defaultCosAttrs: " + attr);
/*     */       }
/*     */     }
/* 197 */     if (defaultCosAttrs.containsKey("zimbraCreateTimestamp")) {
/* 198 */       Assert.assertEquals(defaultCosAttrs.size() - 1, copiedCosAttrs.size());
/*     */     } else {
/* 200 */       Assert.assertEquals(defaultCosAttrs.size(), copiedCosAttrs.size());
/*     */     }
/* 202 */     Assert.assertNull(copiedCosAttrs.get("description"));
/*     */     
/* 204 */     deleteCos(copiedCos);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void getAllCos() throws Exception {
/* 209 */     Cos defaultCos = prov.get(Key.CosBy.name, "default");
/*     */     
/* 211 */     List<Cos> allCos = prov.getAllCos();
/* 212 */     Assert.assertEquals(2L, allCos.size());
/* 213 */     Assert.assertEquals(defaultCos.getId(), ((Cos)allCos.get(0)).getId());
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void renameCos() throws Exception {
/* 218 */     String OLD_COS_NAME = Names.makeCosName(genCosName("old"));
/* 219 */     String NEW_COS_NAME = Names.makeCosName(genCosName("new"));
/*     */     
/* 221 */     Cos cos = createCos(OLD_COS_NAME);
/* 222 */     String cosId = cos.getId();
/*     */     
/* 224 */     prov.renameCos(cos.getId(), NEW_COS_NAME);
/* 225 */     Cos renamedCos = prov.get(Key.CosBy.name, NEW_COS_NAME);
/* 226 */     Assert.assertEquals(cosId, renamedCos.getId());
/*     */     
/* 228 */     deleteCos(cos);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void renameCosToExisting() throws Exception {
/* 233 */     String OLD_COS_NAME = Names.makeCosName(genCosName("old"));
/* 234 */     String NEW_COS_NAME = "default";
/*     */     
/* 236 */     Cos cos = createCos(OLD_COS_NAME);
/* 237 */     String cosId = cos.getId();
/*     */     
/* 239 */     boolean caughtException = false;
/*     */     try {
/* 241 */       prov.renameCos(cos.getId(), NEW_COS_NAME);
/*     */     } catch (AccountServiceException e) {
/* 243 */       if ("account.COS_EXISTS".equals(e.getCode())) {
/* 244 */         caughtException = true;
/*     */       }
/*     */     }
/* 247 */     Assert.assertTrue(caughtException);
/*     */     
/* 249 */     deleteCos(cos);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void domainCos() throws Exception {
/* 254 */     String ATTR_NAME = "zimbraMailQuota";
/*     */     
/* 256 */     String COS1_NAME = genCosName("1");
/* 257 */     String COS2_NAME = genCosName("2");
/* 258 */     String DOMAIN_DEFAULT_COS_NAME = genCosName("domain-default-cos");
/*     */     
/* 260 */     String COS1_VALUE = "10000";
/* 261 */     String COS2_VALUE = "20000";
/* 262 */     String DOMAIN_DEFAULT_COS_VALUE = "30000";
/* 263 */     String ACCOUNT_VALUE = "40000";
/*     */     
/* 265 */     String SYSTEM_DEFAULT_COS_VALUE = prov.get(Key.CosBy.name, "default").getAttr(ATTR_NAME);
/*     */     
/*     */ 
/* 268 */     Map<String, Object> cos1Attrs = Maps.newHashMap();
/* 269 */     cos1Attrs.put(ATTR_NAME, "10000");
/* 270 */     Cos cos1 = createCos(COS1_NAME, cos1Attrs);
/*     */     
/* 272 */     Map<String, Object> cos2Attrs = Maps.newHashMap();
/* 273 */     cos2Attrs.put(ATTR_NAME, "20000");
/* 274 */     Cos cos2 = createCos(COS2_NAME, cos2Attrs);
/*     */     
/* 276 */     Map<String, Object> domainDefaultCosAttrs = Maps.newHashMap();
/* 277 */     domainDefaultCosAttrs.put(ATTR_NAME, "30000");
/* 278 */     Cos domainDefaultCos = createCos(DOMAIN_DEFAULT_COS_NAME, domainDefaultCosAttrs);
/*     */     
/* 280 */     modifyAttr(domain, "zimbraDomainDefaultCOSId", domainDefaultCos.getId());
/*     */     
/* 282 */     Account acct = provUtil.createAccount(genAcctNameLocalPart(), domain);
/* 283 */     String ACCT_NAME = acct.getName();
/*     */     
/*     */ 
/* 286 */     Assert.assertEquals("30000", acct.getAttr(ATTR_NAME));
/*     */     
/*     */ 
/* 289 */     String newValue = String.valueOf(Long.valueOf("30000").longValue() - 1L);
/* 290 */     modifyAttr(domainDefaultCos, ATTR_NAME, newValue);
/* 291 */     Assert.assertEquals(newValue, acct.getAttr(ATTR_NAME));
/*     */     
/*     */ 
/* 294 */     modifyAttr(domainDefaultCos, ATTR_NAME, "30000");
/*     */     
/*     */ 
/*     */ 
/* 298 */     modifyAttr(domain, "zimbraDomainDefaultCOSId", cos1.getId());
/* 299 */     Assert.assertEquals("30000", acct.getAttr(ATTR_NAME));
/*     */     
/*     */ 
/* 302 */     prov.flushCache(CacheEntryType.account, new Provisioning.CacheEntry[] { new Provisioning.CacheEntry(Key.CacheEntryBy.name, ACCT_NAME) });
/* 303 */     acct = prov.get(Key.AccountBy.name, ACCT_NAME);
/* 304 */     Assert.assertEquals("10000", acct.getAttr(ATTR_NAME));
/*     */     
/*     */ 
/*     */ 
/* 308 */     modifyAttr(domain, "zimbraDomainDefaultCOSId", null);
/* 309 */     Assert.assertEquals("10000", acct.getAttr(ATTR_NAME));
/*     */     
/*     */ 
/* 312 */     prov.flushCache(CacheEntryType.account, new Provisioning.CacheEntry[] { new Provisioning.CacheEntry(Key.CacheEntryBy.name, ACCT_NAME) });
/* 313 */     acct = prov.get(Key.AccountBy.name, ACCT_NAME);
/* 314 */     Assert.assertEquals(SYSTEM_DEFAULT_COS_VALUE, acct.getAttr(ATTR_NAME));
/*     */     
/* 316 */     provUtil.deleteAccount(acct);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void acctCos() throws Exception {
/* 321 */     String ATTR_NAME = "zimbraMailQuota";
/*     */     
/* 323 */     String COS1_NAME = genCosName("1");
/* 324 */     String COS2_NAME = genCosName("2");
/* 325 */     String DOMAIN_DEFAULT_COS_NAME = genCosName("domian-default-cos");
/*     */     
/* 327 */     String COS1_VALUE = "10000";
/* 328 */     String COS2_VALUE = "20000";
/* 329 */     String DOMAIN_DEFAULT_COS_VALUE = "30000";
/* 330 */     String ACCOUNT_VALUE = "40000";
/*     */     
/* 332 */     Map<String, Object> cos1Attrs = Maps.newHashMap();
/* 333 */     cos1Attrs.put(ATTR_NAME, "10000");
/* 334 */     Cos cos1 = createCos(COS1_NAME, cos1Attrs);
/*     */     
/* 336 */     Map<String, Object> cos2Attrs = Maps.newHashMap();
/* 337 */     cos2Attrs.put(ATTR_NAME, "20000");
/* 338 */     Cos cos2 = createCos(COS2_NAME, cos2Attrs);
/*     */     
/* 340 */     Map<String, Object> domainDefaultCosAttrs = Maps.newHashMap();
/* 341 */     domainDefaultCosAttrs.put(ATTR_NAME, "30000");
/* 342 */     Cos domainDefaultCos = createCos(DOMAIN_DEFAULT_COS_NAME, domainDefaultCosAttrs);
/*     */     
/* 344 */     modifyAttr(domain, "zimbraDomainDefaultCOSId", domainDefaultCos.getId());
/*     */     
/* 346 */     Account acct = provUtil.createAccount(genAcctNameLocalPart(), domain);
/* 347 */     String ACCT_NAME = acct.getName();
/*     */     
/*     */ 
/* 350 */     modifyAttr(acct, "zimbraCOSId", cos1.getId());
/* 351 */     Assert.assertEquals("10000", acct.getAttr(ATTR_NAME));
/*     */     
/*     */ 
/* 354 */     modifyAttr(acct, "zimbraCOSId", cos2.getId());
/* 355 */     Assert.assertEquals("20000", acct.getAttr(ATTR_NAME));
/*     */     
/*     */ 
/* 358 */     modifyAttr(acct, "zimbraCOSId", null);
/* 359 */     Assert.assertEquals("30000", acct.getAttr(ATTR_NAME));
/*     */     
/*     */ 
/* 362 */     modifyAttr(acct, "zimbraCOSId", cos1.getId());
/* 363 */     Assert.assertEquals("10000", acct.getAttr(ATTR_NAME));
/*     */     
/*     */ 
/* 366 */     modifyAttr(acct, ATTR_NAME, "40000");
/* 367 */     Assert.assertEquals("40000", acct.getAttr(ATTR_NAME));
/*     */     
/*     */ 
/* 370 */     modifyAttr(acct, ATTR_NAME, null);
/* 371 */     Assert.assertEquals("10000", acct.getAttr(ATTR_NAME));
/*     */   }
/*     */   
/*     */   @Test
/*     */   @QA.Bug(bug={67716})
/*     */   public void bug67716() throws Exception
/*     */   {
/* 378 */     String ATTR_REAL_ANME = "zimbraMailQuota";
/* 379 */     String ATTR_LOWERCASE_NAME = ATTR_REAL_ANME.toLowerCase();
/* 380 */     String ATTR_VALUE = "12345";
/* 381 */     Map<String, Object> attrs = Maps.newHashMap();
/* 382 */     attrs.put(ATTR_LOWERCASE_NAME, ATTR_VALUE);
/* 383 */     Cos cos = createCos(genCosName(), attrs);
/* 384 */     Assert.assertEquals(ATTR_VALUE, cos.getAttr(ATTR_REAL_ANME));
/* 385 */     Assert.assertEquals(ATTR_VALUE, cos.getAttr(ATTR_LOWERCASE_NAME));
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/prov/ldap/TestLdapProvCos.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */